package com.lixunhuan.handacount.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lixunhuan.handacount.common.ResultInfo;
import com.lixunhuan.handacount.db.bean.Diary;
import com.lixunhuan.handacount.service.IDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lixunhuan
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/diary")
public class DiaryController {
    private static String AUTH_TOKEN = "Authorization";
    @Autowired
    private IDiaryService diaryService;

    /**
     * 添加日记
     */
    @RequestMapping("add")
    public ResultInfo addDiary(HttpServletRequest httpServletRequest,
                               @NotNull @RequestParam("diary") String diary,
                               @NotNull @RequestParam("title")String title){
        Diary diary1 = new Diary();
        diary1.setUid(httpServletRequest.getHeader(AUTH_TOKEN));
        diary1.setDiary(diary);
        diary1.setTitle(title);
        diaryService.save(diary1);
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("日记添加成功");
    }

    /**
     * 查看日记列表
     */
    @RequestMapping("list")
    public ResultInfo selectDiaryList(HttpServletRequest httpServletRequest, String date){
        return new ResultInfo(ResultInfo.Status.SUCCESS).result(
                diaryService.list(new QueryWrapper<Diary>().eq("uid",httpServletRequest.getHeader(AUTH_TOKEN))
                        .like("add_time",date).orderByDesc("id"))
        );
    }
    /**
     * 查看日记
     */
    @RequestMapping("select")
    public ResultInfo selectDiary(HttpServletRequest httpServletRequest,
                                  @RequestParam("diary_id") Integer diaryId){
        if (StringUtils.isEmpty(httpServletRequest.getHeader(AUTH_TOKEN))){
            return new ResultInfo(ResultInfo.Status.REQUEST_PARAMETER_ERROR);
        }
        return new ResultInfo(ResultInfo.Status.SUCCESS).result(
                diaryService.getById(diaryId)
        );
    }

    /**
     * 删除日记
     */
    @RequestMapping("delete")
    public ResultInfo delDiary(HttpServletRequest httpServletRequest,
                               @RequestParam("diary_id") Integer diaryId){
        if (StringUtils.isEmpty(httpServletRequest.getHeader(AUTH_TOKEN))){
            return new ResultInfo(ResultInfo.Status.REQUEST_PARAMETER_ERROR);
        }
        diaryService.removeById(diaryId);
        return new ResultInfo(ResultInfo.Status.SUCCESS);
    }

    /**
     * 修改日记
     * @param httpServletRequest 请求
     * @param diaryId   日记id
     * @param diary     日记
     * @param title     标题
     * @return          结果
     */
    @RequestMapping("update")
    public ResultInfo update(HttpServletRequest httpServletRequest,
                             @RequestParam("diary_id") Integer diaryId,
                             @NotNull @RequestParam("diary") String diary,
                             @NotNull @RequestParam("title")String title){
        if (StringUtils.isEmpty(httpServletRequest.getHeader(AUTH_TOKEN))){
            return new ResultInfo(ResultInfo.Status.REQUEST_PARAMETER_ERROR);
        }
        Diary diary1 = new Diary();
        diary1.setTitle(title);
        diary1.setDiary(diary);
        diaryService.update(diary1,new QueryWrapper<Diary>().eq("id",diaryId));
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("修改成功");
    }
}
